package com.chen.shopping_user_customer_api.controller;

import com.chen.shopping_common.pojo.ShoppingUser;
import com.chen.shopping_common.result.BaseResult;
import com.chen.shopping_common.service.MessageService;
import com.chen.shopping_common.service.ShoppingUserService;
import com.chen.shopping_common.utils.RandomUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/shoppingUser")
public class ShoppingUserController {

    @DubboReference
    private MessageService messageService;
    @DubboReference
    private ShoppingUserService shoppingUserService;

    @GetMapping("/sendMessage")
    public BaseResult sendMessage(String phone){
        //生成四位数随机验证码
        String checkCode = RandomUtil.buildCheckNode(4);
        //发送短信
        BaseResult baseResult = messageService.sendMessage(phone, checkCode);
        //发送成功，将手机号和验证码存入Redis中
        if (200 == baseResult.getCode()){
            shoppingUserService.saveRegisterCheckNode(phone,checkCode);
            return BaseResult.ok();
        }else {
            //发送失败，返回结果
            return baseResult;
        }

    }

    @GetMapping("/registerCheckCode")
    public BaseResult registerCheckCode(String phone,String checkCode){
        shoppingUserService.registerCheckCode(phone, checkCode);
        return BaseResult.ok();
    }

    @PostMapping("/register")
    public BaseResult register(@RequestBody ShoppingUser shoppingUser){
        shoppingUserService.register(shoppingUser);
        return BaseResult.ok();
    }

    @PostMapping("/loginPassword")
    public BaseResult loginPassword(@RequestBody ShoppingUser shoppingUser){
        String sign = shoppingUserService.loginPassword(shoppingUser.getUsername(), shoppingUser.getPassword());
        return BaseResult.ok(sign);
    }

    @GetMapping("/sendLoginCheckCode")
    public BaseResult sendLoginMessage(String phone){
        //检验手机号状态
        shoppingUserService.checkPhone(phone);
        //生成四位数随机验证码
        String checkCode = RandomUtil.buildCheckNode(4);
        //发送短信
        BaseResult baseResult = messageService.sendMessage(phone, checkCode);
        //发送成功，将手机号和验证码存入Redis中
        if (200 == baseResult.getCode()){
            shoppingUserService.saveLoginCheckCode(phone,checkCode);
            return BaseResult.ok();
        }else {
            //发送失败，返回结果
            return baseResult;
        }

    }

    @PostMapping("/loginCheckCode")
    public BaseResult loginCheckCode(String phone,String checkCode){
        String sign = shoppingUserService.loginCheckCode(phone, checkCode);
        return BaseResult.ok(sign);
    }


    @GetMapping("/getName")
    public BaseResult<String> getName(@RequestHeader("Authorization") String Authorization){
        String token = Authorization.replace("Bearer ","");
        String name = shoppingUserService.getName(token);
        return BaseResult.ok(name);
    }
}
